<?php

/**
 * @copyright 2012
 */
require_once("SqlUtils.php");
require_once("Account.php");
require_once("Turnover.php");

$type = $_GET["type"];
$remark = $_GET["remark"];
$time = $_GET["time"];
$money = $_GET["money"];
$from = $_GET["from"];
$to = $_GET["to"];
$catid = $_GET["catid"];

$paramArray["type"] = $_GET["type"];
$paramArray["remark"] = $_GET["remark"];
$paramArray["time"] = $_GET["time"];
$paramArray["money"] = $_GET["money"];
$paramArray["from"] = $_GET["from"];
$paramArray["to"] = $_GET["to"];
$paramArray["catid"] = $_GET["catid"];


////////////////////////////////////////////////////////
$newTurnoverID = AddTurnover($paramArray);

if($newTurnoverID == -1)
{
    echo "AddTurnover error";
    exit;
}

////////////////////////////////////////////////////////
 $acc_from = new Account();
 $acc_to = new Account();
if($type == 0)
{
    $acc_from->Init($paramArray["from"]); 
    $acc_from->money -= $paramArray["money"];
    $acc_from->Update();
} else if($type == 1)
{
    $acc_to->Init($paramArray["to"]); 
    $acc_to->money += $paramArray["money"];
    $acc_to->Update();
} else if($type == 2)
{
    $acc_from->Init($paramArray["from"]); 
    $acc_from->money -= $paramArray["money"];
    $acc_from->Update();
    
    $acc_to->Init($paramArray["to"]); 
    $acc_to->money += $paramArray["money"];
    $acc_to->Update();
}


////////////////////////////////////////////////////////

  $doc = new DOMDocument();
  $doc->formatOutput = true;
  
  $turnoversNode = $doc->createElement("turnovers");
  $doc->appendChild($turnoversNode);
  
  $turnover = new Turnover();
  $turnover->Init($newTurnoverID);
  $turnoverNode = $turnover->GetXMLNode($doc);
  $turnoversNode->appendChild($turnoverNode);
  
  $accountsNode = $doc->createElement("accounts");
  if($acc_from->id)
  {
    $fromXMLNode = $acc_from->GetXMLNode($doc);
    $accountsNode->appendChild($fromXMLNode);
  }
  if($acc_to->id)
  {
    $fromXMLNode = $acc_to->GetXMLNode($doc);
    $accountsNode->appendChild($fromXMLNode);
  }
  $doc->appendChild($accountsNode);

  echo $doc->saveXML();

?>